define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'merchant/index' + location.search,
                    add_url: 'merchant/add',
                    edit_url: 'merchant/edit',
                    del_url: 'merchant/del',
                    multi_url: 'merchant/multi',
                    check_url: 'merchant/check',
                    table: 'merchant',
                }
            });

            var table = $("#table");
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'merchant_phone', title: __('Merchant_phone'), operate: 'like'},
                        {field: 'merchant_name', title: __('Merchant_name'), operate: 'like'},
                        {field: 'shop_name', title: __('Shop_name'),operate: 'LIKE'},
                        {field: 'shop_balance', title: __('Shop_balance')},
                        {field: 'shop_logo', title: __('Shop_logo'), events: Table.api.events.image, formatter: Table.api.formatter.image,operate: false},
                        {field: 'open_time', title: __('Open_time'), operate: false},
                        {field: 'night_time', title: __('Night_time'), operate: false},
                        {field: 'city', title: __('地址'),operate: 'like',formatter: function (value, row, index) {
                                return row.province + row.city + row.area + row.street;
                            }},
                        {field: 'stay_time', title: __('Stay_time'), operate:false},
                        {field: 'status', title: __('Status'), searchList: Config.statuslist, formatter: Table.api.formatter.toggle,yes:1,no:2},
                        {field: 'examine_status', title: __('Examine_status'), searchList: Config.checkStatuslist, formatter: Table.api.formatter.label},
                        {field: 'signing_time', title: __('Signing_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime, datetimeFormat:'YYYY-MM-DD'},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {
                            field: 'buttons',
                            width: "120px",
                            title: __('车辆管理'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'addtabs',
                                    text: function (row) {
                                        return '商家车辆(' + row.merchant_car_count + ')';
                                    },
                                    title: __('商家车辆'),
                                    icon: 'fa fa-list',
                                    classname: 'btn btn-xs btn-info btn-dialog',
                                    url: function (row) {
                                        return 'merchant_car/index?merchant_id=' + row.id;
                                    }
                                },
                            ],
                            formatter: Table.api.formatter.buttons
                        },
                        {
                            field: 'buttons',
                            width: "120px",
                            title: '区域价格设置',
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'addtabs',
                                    text: function (row) {
                                        return '区域价格设置(' + row.merchant_area_count + ')';
                                    },
                                    title: __('区域价格设置'),
                                    icon: 'fa fa-list',
                                    classname: 'btn btn-xs btn-info btn-dialog',
                                    url: function (row) {
                                        return 'merchant_area/index?merchant_id=' + row.id;
                                    }
                                }
                            ],
                            formatter: Table.api.formatter.buttons
                        },
                        {
                            field: 'operate', title: __('Operate'), table: table,
                            events: Table.api.events.operate,
                            buttons: [{
                                name: 'detail',
                                text: __('Detail'),
                                icon: 'fa fa-list',
                                classname: 'btn btn-info btn-xs btn-detail btn-dialog',
                                url: 'merchant/detail'
                            }],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'merchant/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '130px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'merchant/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'merchant/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        check: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});